import React from 'react';
import {Card, Button, Table, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {salePlanAssembleMethod, baseDataStatus} from '../Enum';
import routes from './routes';
import {TABLE, DATETIME_FORMAT} from '../constants';
import {PERMISSION} from './constants';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {getDetail, abandonData} from './api';
import {injectIntl} from './intl';
import {hasPermissions} from './utils';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details.map(d => Object.assign({}, d, {
                            order: res.data.details.findIndex(x => x.id === d.id) + 1,
                        })),
                    });
                    
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    onClickAbandon = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.state.data.rowVersion
        };
        abandonData(this.state.data.id, data)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {data, details} = this.state;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(data.options, PERMISSION.update))
            operations.push(<Button key="update" href={`#${routes.update.format(this.props.id)}`} size="small">{
                formatMessage({
                    id: 'detailPanel.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(data.options, PERMISSION.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandon}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        const column = [
            {
                title: formatMessage({
                    id: 'detailPanel.column.businessType',
                    defaultMessage: '装配方式'
                }),
                dataIndex: 'businessType',
                render: text => conventEnumValueToString(salePlanAssembleMethod, text)
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.versionCode',
                    defaultMessage: '版型编号'
                }),
                dataIndex: 'versionCode'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.versionName',
                    defaultMessage: '版型名称'
                }),
                dataIndex: 'versionName'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.modelCode',
                    defaultMessage: '车型代码'
                }),
                dataIndex: 'modelCode'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.modelName',
                    defaultMessage: '车型名称'
                }),
                dataIndex: 'modelName'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.materialGroupKDBOMCode',
                    defaultMessage: 'KDBOM号或参考物料组'
                }),
                dataIndex: 'materialGroupKDBOMCode'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.isNewModel',
                    defaultMessage: '是否新车型'
                }),
                dataIndex: 'isNewModel',
                render: text => text ? formatMessage({
                    id: 'detailPanel.column.isExpectedListYes',
                    defaultMessage: '是'
                }) : formatMessage({
                    id: 'detailPanel.column.isExpectedListNo',
                    defaultMessage: '否'
                })
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n1SalesTargetQuantity',
                    defaultMessage: 'N+1计划数'
                }),
                dataIndex: 'n1SalesTargetQuantity'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n2SalesTargetQuantity',
                    defaultMessage: 'N+2计划数'
                }),
                dataIndex: 'n2SalesTargetQuantity'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n3SalesTargetQuantity',
                    defaultMessage: 'N+3计划数'
                }),
                dataIndex: 'n3SalesTargetQuantity'
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n4SalesTargetQuantity',
                    defaultMessage: 'N+4计划数'
                }),
                dataIndex: 'n4SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n5SalesTargetQuantity',
                    defaultMessage: 'N+5计划数'
                }),
                dataIndex: 'n5SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n6SalesTargetQuantity',
                    defaultMessage: 'N+6计划数'
                }),
                dataIndex: 'n6SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n7SalesTargetQuantity',
                    defaultMessage: 'N+7计划数'
                }),
                dataIndex: 'n7SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n8SalesTargetQuantity',
                    defaultMessage: 'N+8计划数'
                }),
                dataIndex: 'n8SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n9SalesTargetQuantity',
                    defaultMessage: 'N+9计划数'
                }),
                dataIndex: 'n9SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n10SalesTargetQuantity',
                    defaultMessage: 'N+10计划数'
                }),
                dataIndex: 'n10SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n11SalesTargetQuantity',
                    defaultMessage: 'N+11计划数'
                }),
                dataIndex: 'n11SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.n12SalesTargetQuantity',
                    defaultMessage: 'N+12计划数'
                }),
                dataIndex: 'n12SalesTargetQuantity',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.total',
                    defaultMessage: '总计'
                }),
                dataIndex: 'total',
            },
            {
                title: formatMessage({
                    id: 'detailPanel.column.remark',
                    defaultMessage: '备注'
                }),
                dataIndex: 'remark',
            },
        ];
        return (
            <div>
                <div className="detail">
                    <Card loading={this.state.loading}>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'detailPanel.card.title',
                                defaultMessage: 'KD生产计划管理'
                            })
                        }</Col>
                        <Col className={styles.optionBtn}>{operations}</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.planYear',
                                defaultMessage: '计划年份'
                            })}>{data.planYear}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.brandCode',
                                defaultMessage: '品牌'
                            })}>{data.brandCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.marketDepartmentName',
                                defaultMessage: '大区名称'
                            })}>{data.marketDepartmentName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.country',
                                defaultMessage: '国家'
                            })}>{data.country}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.dealerCode',
                                defaultMessage: '总代编号'
                            })}>{data.dealerCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(baseDataStatus, data.status)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.creatorName',
                                defaultMessage: '创建人',
                            })}>{data.creatorName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.createTime',
                                defaultMessage: '创建时间',
                            })}>{formatDateTime(data.createTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.abandonerName',
                                defaultMessage: '作废人',
                            })}>{data.abandonerName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.abandonTime',
                                defaultMessage: '作废时间',
                            })}>{formatDateTime(data.abandonTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.remark',
                                defaultMessage: '备注',
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card title={this.props.intl.formatMessage({
                        id: 'detailPanel.table.details',
                        defaultMessage: 'KD生产计划清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={column}
                            rowKey="id"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </Card>
                </div>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
